#include "icondialog.h"
#include "qdebug.h"
#include "ui_icondialog.h"

#include <QDir>
#include <QPixmap>
#include <QPixmap>

IconDialog::IconDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IconDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint); //不显示边框
    QPixmap search(":/svg/res/svg/search.svg");
    ui->label->setPixmap(search.scaled(15,15));

    init();
}

IconDialog::~IconDialog()
{
    delete ui;
}

void IconDialog::init()
{
    QString resourcePath = ":/svg/res/svg";
    QDir dir(resourcePath);
    QFileInfoList fileList = dir.entryInfoList();
    ui->tableWidget->setRowCount(fileList.size() % 3 == 0 ?
                    fileList.size() / 3 : fileList.size() / 3 + 1);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setColumnWidth(0,130);
    ui->tableWidget->setColumnWidth(1,130);
    ui->tableWidget->setColumnWidth(2,130);
    for(int i = 0;i< fileList.size();i++){
        if (fileList[i].isFile() && fileList[i].suffix().toLower() == "svg") {
            QString iconPath = fileList[i].filePath();
            QPixmap icon(iconPath);
            QTableWidgetItem  *item = new QTableWidgetItem;
            item->setIcon(icon);
            item->setText(fileList[i].fileName().mid(0,fileList[i].fileName().indexOf(".")));
            int row = i / 3;
            int column = i % 3;
            ui->tableWidget->setItem(row,column,item);
        }
    }
}

void IconDialog::on_tableWidget_cellClicked(int row, int column)
{
    QString iconName = ui->tableWidget->item(row,column)->text();
    emit checkIcon(iconName);
    close();
}

